package com.yanqu.road.server.manger.activity.suoyao;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.suoyao.config.*;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.SuoYaoBusiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.suoyao.SuoYaoModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.RandomHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class SuoYaoMgr extends TempMgr {

    private static SuoYaoConfig suoYaoConfig;

    // 记录名称，便于活动结束后发邮件
    private static String activityName = "";

    @Override
    public boolean save() {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadActivityData() {
        getLogger().info("reload suoyao activity start");
        // 获取活动
        ActivityInfo tmpActivityInfo;
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.SuoYaoActivity.getValue());
        if (openActivityInfoList.size() <= 0) {
            getLogger().info("no suoyao activity in show time");
            // 没有活动：读取最后一个活动名称
            if (suoYaoConfig == null) {
           //     activityName = ActivityMgr.getLastExpireActivityName(GameServer.getInstance().getServerId(), eActivityType.SuoYaoActivity.getValue(), 1);
            }
            return;
        }
        tmpActivityInfo = openActivityInfoList.get(0);

        SuoYaoConfig tmpConfig = new SuoYaoConfig();
        tmpConfig.setActivityInfo(tmpActivityInfo);
        tmpConfig.setActivityId(tmpActivityInfo.getActivityId());

        // 其他配置
        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityInfo.getActivityId());

        tmpConfig.setScoreItemId(activityConfigMap.get(eActivityConfigType.SUOYAO_SCORE_ITEM_ID.getName()).getIntValue());

        tmpConfig.setExploreItemId(activityConfigMap.get(eActivityConfigType.SUOYAO_EXPLORE_ITEM_ID.getName()).getIntValue());

        tmpConfig.setSwordItemId(0);

        tmpConfig.setExploreCountList(activityConfigMap.get(eActivityConfigType.SUOYAO_EXPLORE_COST_COUNT_LIST.getName()).getIntListValue(";"));

//        List<Integer> boxCountParam = activityConfigMap.get(eActivityConfigType.SUOYAO_HIDE_BOX_LUCKY_PARAM.getName()).getIntListValue(";");
//        tmpConfig.setHideBoxTimes(boxCountParam.get(0));
//        tmpConfig.setHideBoxCount(boxCountParam.get(1) * tmpActivityInfo.getServerIdList().size());
//        tmpConfig.setLuckyPercent(boxCountParam.get(2));
//
//        List<Integer> additionParam = activityConfigMap.get(eActivityConfigType.SUOYAO_HIDE_BOX_ADDITION_PARAM.getName()).getIntListValue(";");
//        tmpConfig.setHideBoxAddTimes(additionParam.get(0));
//        if (tmpConfig.getHideBoxAddTimes() <= tmpConfig.getHideBoxTimes()) {
//            getLogger().error("SuoYaoMgr config SUOYAO_HIDE_BOX_ADDITION_PARAM error! activityId={}", tmpConfig.getActivityId());
//            return;
//        }
//        tmpConfig.setHideBoxAddWeight(additionParam.get(1));

        tmpConfig.setNoticeCount(20);


        // 塔的奖励 配置
        Map<Integer, Map<Integer, SuoYaoRewardConfig>> tmpConfigMap = SuoYaoBusiness.getSuoYaoRewardConfigMap(tmpActivityInfo.getActivityId());
        tmpConfig.setFloorMap(tmpConfigMap);

        // 图鉴配置
        List<SuoYaoBookConfig> bookConfigList = SuoYaoBusiness.getSuoYaoBookConfigList(tmpActivityInfo.getActivityId());
        tmpConfig.setBookConfigList(bookConfigList);

        Map<Integer, SuoYaoTopRewardConfig> suoYaoTopRewardConfigMap = SuoYaoBusiness.getSuoYaoTopRewardConfigMap(tmpActivityInfo.getActivityId());
        tmpConfig.setTopRewardConfigMap(suoYaoTopRewardConfigMap);


        Map<Integer, SuoYaoViewBookConfig> suoYaoViewBookConfigMap = SuoYaoBusiness.getSuoYaoViewBookConfigMap(tmpActivityInfo.getActivityId());
        tmpConfig.setViewBookConfigMap(suoYaoViewBookConfigMap);

        suoYaoConfig = tmpConfig;
        activityName = suoYaoConfig.getActivityInfo().getActivityName();

        getLogger().info("reload suoyao activity finish");

        reloadPlayerData();
    }

    public static ActivityInfo getActivityInfo() {
        return suoYaoConfig == null ? null : suoYaoConfig.getActivityInfo();
    }

    public static SuoYaoConfig getSuoYaoConfig() {
        return suoYaoConfig;
    }

    private static void reloadPlayerData() {
        for (GamePlayer player : GamePlayerMgr.getCopyPlayerMap().values()) {
            SuoYaoModule suoYaoModule = player.getModule(SuoYaoModule.class);
            suoYaoModule.initUserData();
            suoYaoModule.syncSuoYaoConfig();
            suoYaoModule.syncUserData();
        }
    }

    public static String getActivityName() {
        return activityName;
    }

    /**
     * 抽盲盒（正常抽）
     *
     * @param commonTimes 已有普通奖励次数
     */
    public static SuoYaoBoxItemConfig drawBox(int commonTimes) {
        if (suoYaoConfig == null) {
            return null;
        }
        // 获取隐藏奖励加成值
        int addWeight = commonTimes * suoYaoConfig.getHideBoxAddWeight();

        // 获取权重
        List<SuoYaoBoxItemConfig> configList = suoYaoConfig.getBoxItemConfigList();
        List<Integer> weightList = new ArrayList<>();
        for (SuoYaoBoxItemConfig itemConfig : configList) {
            if (itemConfig.isHide()) {
                // 隐藏款加成
                weightList.add(itemConfig.getWeight() + addWeight);
            } else {
                weightList.add(itemConfig.getWeight());
            }
        }

        // 抽
        Random random = new Random();
        int index = RandomHelper.getRandomIndexByWeight(weightList, random);
        return configList.get(index);
    }

    /**
     * 抽盲盒
     *
     * @param isHide true中隐藏，false中非隐藏
     */
    public static SuoYaoBoxItemConfig drawBoxByHide(boolean isHide) {
        List<SuoYaoBoxItemConfig> configList = suoYaoConfig.getBoxItemConfigList();

        // 获取权重
        List<Integer> weightList = new ArrayList<>();
        List<SuoYaoBoxItemConfig> hideList = new ArrayList<>();
        for (SuoYaoBoxItemConfig itemConfig : configList) {
            if (isHide == itemConfig.isHide()) {
                weightList.add(itemConfig.getWeight());
                hideList.add(itemConfig);
            }
        }

        // 抽
        Random random = new Random();
        int index = RandomHelper.getRandomIndexByWeight(weightList, random);
        return hideList.get(index);
    }

    public static SuoYaoTopRewardConfig getTopRewardConfig(int rewardId){
        if (suoYaoConfig == null){
            return null;
        }
        Map<Integer, SuoYaoTopRewardConfig> topRewardConfigMap = suoYaoConfig.getTopRewardConfigMap();
        if (topRewardConfigMap == null ||!topRewardConfigMap.containsKey(rewardId)){
            return null;
        }
        return topRewardConfigMap.get(rewardId);
    }

    public static String getTopReward(int rewardId,int index, boolean isSelected){
        SuoYaoTopRewardConfig topRewardConfig = getTopRewardConfig(rewardId);
        if (topRewardConfig == null){
            return null;
        }
        return isSelected? topRewardConfig.getSelectedLimit().get(index) : topRewardConfig.getRewardList().get(index);
    }



}
